package com.example.user.myhjjf_app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Content;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_pt;
import com.example.user.myhjjf_app.model.Model_ptsb;
import com.example.user.myhjjf_app.presenter.Presenter_Cintent_adapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.adapter.abslistview.MultiItemTypeAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_Pzlist extends AppCompatActivity {

    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.btn_add)
    LinearLayout btnAdd;
    @BindView(R.id.btn_menu2)
    LinearLayout btnMenu2;
    @BindView(R.id.list_pt_item)
    ListView listPtItem;
    String type,name,id;
    MultiItemTypeAdapter adapter;
    Model_Content content;
    List<Model_Content.ValueBean> beanList = new ArrayList<>();
    Class aClass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__pzlist);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        type = Model_Regional.getRegional().getType();

        name = Model_Regional.getRegional().getName();
        id = Model_Regional.getRegional().getSiteId();
        show();
        txTitle.setText(name+"列表");
        OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/list.do"))
                .tag(this)
                .params("id", id)
                .params("type",type)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        content = JSON.parseObject(response.body(), Model_Content.class);
                        for (int i = 0; i < content.getValue().size(); i++) {
                            beanList.add(content.getValue().get(i));
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
        adapter = new MultiItemTypeAdapter(this, beanList);
        adapter.addItemViewDelegate(new Presenter_Cintent_adapter(type));
        listPtItem.setAdapter(adapter);
        listPtItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(Activity_Pzlist.this,aClass);
                Bundle bundle = new Bundle();
                bundle.putString("type","select");
                bundle.putSerializable("bean", (Serializable) beanList.get(i));
                intent.putExtras(bundle);
                startActivityForResult(intent,0);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            beanList = new ArrayList<>();
            OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/list.do"))
                    .tag(this)
                    .params("id", id)
                    .params("type",type)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            content = JSON.parseObject(response.body(), Model_Content.class);
                            for (int i = 0; i < content.getValue().size(); i++) {
                                beanList.add(content.getValue().get(i));
                            }
                            adapter.notifyDataSetChanged();
                        }
                    });
            adapter = new MultiItemTypeAdapter(this, beanList);
            adapter.addItemViewDelegate(new Presenter_Cintent_adapter(type));
            listPtItem.setAdapter(adapter);
        }
    }


    @OnClick({R.id.btn_back, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_add:
                Bundle bundle = new Bundle();
                bundle.putString("type","add");
                Intent intent = null;
                switch (type){
                    case "station":
                        intent = new Intent(Activity_Pzlist.this,Activity_stationAdd.class);
                        break;
                    case "yjf":
                        intent = new Intent(Activity_Pzlist.this,Activity_jyfAdd.class);
                        break;
                    case "jzsd":
                        intent = new Intent(Activity_Pzlist.this,Activity_jzsdAdd.class);
                        break;
                    case "jlpdx":
                        intent = new Intent(Activity_Pzlist.this,Activity_pdxAdd.class);
                        break;
                    case "kgdy":
                        intent = new Intent(Activity_Pzlist.this,Activity_dyAdd.class);
                        break;
                    case "flx":
                        intent = new Intent(Activity_Pzlist.this,Activity_flxAdd.class);
                        break;
                    case "yj":
                        intent = new Intent(Activity_Pzlist.this,Activity_yjAdd.class);
                        break;
                    case "xdc":
                        intent = new Intent(Activity_Pzlist.this,Activity_xdcAdd.class);
                        break;
                    case "kt":
                        intent = new Intent(Activity_Pzlist.this,Activity_ktAdd.class);
                        break;
                    case "zntf":
                        intent = new Intent(Activity_Pzlist.this,Activity_tfxtAdd.class);
                        break;
                    case "dhjk":
                        intent = new Intent(Activity_Pzlist.this,Activity_dhjkAdd.class);
                        break;
                    case "ptn3900":
                        intent = new Intent(Activity_Pzlist.this,Activity_ptn.class);
                        break;
                    case "bbu":
                        intent = new Intent(Activity_Pzlist.this,Activity_buuAdd.class);
                        break;
                }
                intent.putExtras(bundle);
                startActivityForResult(intent,0);
                break;
        }
    }
    public void show(){
        switch (type){
            case "station":
                aClass =  Activity_stationAdd.class;
                break;
            case "yjf":
                aClass =  Activity_jyfAdd.class;
                break;
            case "jzsd":
                aClass =  Activity_jzsdAdd.class;
                break;
            case "jlpdx":
                aClass =  Activity_pdxAdd.class;
                break;
            case "kgdy":
                aClass =  Activity_dyAdd.class;
                break;
            case "flx":
                aClass =  Activity_flxAdd.class;
                break;
            case "yj":
                aClass =  Activity_yjAdd.class;
                break;
            case "xdc":
                aClass =  Activity_xdcAdd.class;
                break;
            case "kt":
                aClass =  Activity_ktAdd.class;
                break;
            case "zntf":
                aClass =  Activity_tfxtAdd.class;
                break;
            case "dhjk":
                aClass =  Activity_dhjkAdd.class;
                break;
            case "ptn3900":
                aClass =  Activity_ptn.class;
                break;
            case "bbu":
                aClass =  Activity_buuAdd.class;
                break;
        }
    }
}
